var app = angular.module('webApp', ['ui.router', 'oc.lazyLoad', 'ngCookies','iService','iDirective','Filter']);
// 页面路由
app.config(['$controllerProvider','$stateProvider','$urlRouterProvider', '$locationProvider','$ocLazyLoadProvider',function($controllerProvider,$stateProvider,$urlRouterProvider,$locationProvider,$ocLazyLoadProvider) {
    //动态拆分控制器，指令，过滤器，工厂，服务
    app.register = {
        controller: $controllerProvider.register,
        /*directive: $compileProvider.directive,
        filter: $filterProvider.register,
        factory: $provide.factory,
        service: $provide.service,
        constant: $provide.constant*/
    };
    //路由状态重写
    var loadResolve = function (files,options) {
        var loadPlugin = {
            deps: ['$ocLazyLoad', function($ocLazyLoad) {
                return $ocLazyLoad.load({
                    files: files
                });
            }]
        }
        if(options){
            angular.forEach(options,function (value,key) {
                loadPlugin[key] = value;
            })
        }
        return loadPlugin;
    }
    //动态路由
    $stateProvider
    .state('home', {    //首页
        url:'/',
        templateUrl: 'views/home.html',
        controller:'homeCtrl',
        resolve : loadResolve([__uri('../css/home.less')])
    })
    .state('mail', {    //展示页
        url: '/mail',
        templateUrl: 'views/mail.html',
        controller: 'mailCtrl',
        resolve: loadResolve([__uri('../css/mail.less')])
    })
    .state('show', {    //展示页
        url: '/show/:id/:sign',
        templateUrl: 'views/show.html',
        controller: 'showCtrl',
        resolve: loadResolve([__uri('../css/show.less')])
    })
    $urlRouterProvider.otherwise('/');
    // $locationProvider.html5Mode(true);
    $locationProvider.hashPrefix('');
}]);


// 在所有route跳转前进行登录检查，目标页面需要登录且用户未登录，则跳转到登录页面
app.run(['$rootScope', '$cookies', '$location', function($rootScope,$cookies,$location) {
    $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
        // $cookies.wx_h5_wxuid= 'chen'
        window.scrollTo(0,0);
        // 加载wx的key
        if($config.iswx()){
            if(toState.name=='show'){
                $config.wxShare('http://h5.oeeee.com/h5/motherday/#/show/'+toParams.id+'/'+toParams.sign);
            }else if(toState.name=='home'){
                $config.wxShare('http://h5.oeeee.com/h5/motherday/#/', '妈妈，不用您挂心就是我最大的孝心', '母亲节我给妈妈送贺卡');
            }
        }
    });
    $rootScope.isFirstIn = false;
}]);



app.controller('appCtrl',['$scope','$rootScope','$location','$http', '$timeout', function ($scope,$rootScope,$location,$http, $timeout) {

    //设置title
    $scope.update_title = function(title) {
        var body = document.getElementsByTagName('body')[0];
        document.title = title;
        var iframe = document.createElement("iframe");
        iframe.setAttribute("src", "/empty.png");

        iframe.addEventListener('load', function() {
            setTimeout(function() {
                iframe.removeEventListener('load');
                document.body.removeChild(iframe);
            }, 0);
        });
        document.body.appendChild(iframe);
    };
}]);